package com.Brewing.springboot.controller;

import java.util.Set;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.Brewing.springboot.common.BrewingJSONResult;
import com.Brewing.springboot.common.MD5Utils;
import com.Brewing.springboot.entity.TpUsers.TpUsers;
import com.Brewing.springboot.entity.vo.TpUsersVO;
import com.Brewing.springboot.service.TpUsers.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Company: </p>
 * @author  庄磊
 * @date	2018年11月30日 下午4:51:51 
 * @version 1.0
 */
@RestController
@Api(value="LoginRegist-Api",tags= {"LoginRegist-Api"})
public class RegistLoginController extends BasicController{
	
	@Autowired
	private UserService userService;
	
	@ApiOperation(value="用户注册",notes="用户注册的接口")
	@PostMapping("/regist")
	public BrewingJSONResult regist(@RequestBody TpUsers tpUsers) throws Exception {
		//1.判断用户名和密码是否为空
		if(StringUtils.isBlank(tpUsers.getUsername()) || StringUtils.isBlank(tpUsers.getPassword())) {
			return BrewingJSONResult.errorMsg("用户名和密码不能为空");
		}
		//2.判断用户是否存在
		boolean usernameIsExist = userService.queryUsernameIsExist(tpUsers.getUsername());
		
		//3保存用户
		if(!usernameIsExist) {
			tpUsers.setNickname(tpUsers.getUsername());
			tpUsers.setPassword(MD5Utils.getMD5Str(tpUsers.getPassword()));
			tpUsers.setFaceImage("/moren.jpg");
			tpUsers.setFansCounts(0);
			tpUsers.setReceiveLikeCounts(0);
			tpUsers.setFollowCounts(0);
			tpUsers.setFaceImage(null);
			userService.saveUser(tpUsers);
		}else {
			return BrewingJSONResult.errorMsg("用户名已经存在，请换一个再试");
		}
		tpUsers.setPassword("");
		TpUsersVO usersVO = setUserRedisSessionToken(tpUsers);
		return BrewingJSONResult.ok(usersVO);
	}
	public TpUsersVO setUserRedisSessionToken(TpUsers userModel) {
		String uniqueToken =UUID.randomUUID().toString();
		redis.set(USER_REDIS_SESSION + ":" + userModel.getId(), uniqueToken,1000 * 60 * 30);
		
		TpUsersVO usersVO = new TpUsersVO();
		BeanUtils.copyProperties(userModel,usersVO);
		usersVO.setUserToken(uniqueToken);
		return usersVO;
	}
	@ApiOperation(value="用户登陆",notes="用户登陆的接口")
	@PostMapping("/login")
	public BrewingJSONResult login(@RequestBody TpUsers tpUsers) throws Exception{
		String username = tpUsers.getUsername();
		String password = tpUsers.getPassword();
		
		//1.判断用户名和密码是否为空
		if(StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
			return BrewingJSONResult.errorMsg("用户名和密码不能为空");
		}
		//2.判断用户是否存在
		TpUsers userResult = userService.queryUserForLogin(username, MD5Utils.getMD5Str(password));
		if(userResult!=null) {
			userResult.setPassword("");
			TpUsersVO usersVO = setUserRedisSessionToken(userResult);
			return BrewingJSONResult.ok(usersVO);
		}
		else {
			return BrewingJSONResult.errorMsg("用户名或密码不正确...");
		}
	}
	
	@ApiOperation(value="用户注销", notes="用户注销的接口")
	@ApiImplicitParam(name="userId", value="用户id", required=true, 
						dataType="String", paramType="query")
	@PostMapping("/logout")
	public BrewingJSONResult logout(String userId) throws Exception {
		redis.del(USER_REDIS_SESSION + ":" + userId);
		return BrewingJSONResult.ok();
	}
}
